package com.gxcc3.ygps.web.controller.check;


import com.gxcc3.ygps.common.annotation.Log;
import com.gxcc3.ygps.common.core.controller.BaseController;
import com.gxcc3.ygps.common.core.domain.AjaxResult;
import com.gxcc3.ygps.common.core.domain.entity.SysCheckDetection;
import com.gxcc3.ygps.common.core.page.TableDataInfo;
import com.gxcc3.ygps.common.enums.BusinessType;
import com.gxcc3.ygps.common.utils.poi.ExcelUtil;
import com.gxcc3.ygps.common.utils.uuid.IdUtils;
import com.gxcc3.ygps.system.service.ISysCheckDetectionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 检测管理 检测列表
 */
@RestController
@RequestMapping("/check/detection")
public class SysCheckDetectionController extends BaseController {


    @Resource
    ISysCheckDetectionService detectionService;



    /**
     * 车辆列表
     */
    @PreAuthorize("@ss.hasPermi('check:detection:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCheckDetection detection)
    {
        startPage();
        detection.setCompanyId(getCompanyId());
        List<SysCheckDetection> list = detectionService.selectDetectionList(detection);
        return getDataTable(list);
    }



    /**
     * 新增车辆
     */
    @Log(title = "检测管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('check:detection:add')")
    public AjaxResult add(@Validated @RequestBody SysCheckDetection detection){
        detection.setCompanyId(getCompanyId());
        detection.setCreateBy(getUsername());
        detection.setReportStatus("1");
        String sampleNo = IdUtils.randomSampleNo();
        detection.setReportSampleNo(sampleNo);
        detection.setReportSampleBatch(sampleNo);
        detection.setReportCheckBatchNo(sampleNo);
        detection.setDataSource("web_input");
        return toAjax(detectionService.insertDetection(detection));
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 模板路径（resource 下）
//        String filePath = "upload-template/detection_template.xlsx";  // 例如 resources/upload/template.xlsx
//        ClassPathResource resource = new ClassPathResource(filePath);
//
//        if (!resource.exists()) {
//            throw new RuntimeException("模板文件不存在: " + filePath);
//        }
//
//        // 设置下载响应头
//        String fileName = URLEncoder.encode("导入模板.xlsx", "UTF-8");
//        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
//
//        // 读取文件并写入响应
//        try (InputStream in = resource.getInputStream()) {
//            StreamUtils.copy(in, response.getOutputStream());
//        }
//        List<ExcelColumn> columnList  = detectionService.importTemplate();
//        ExcelUtils excelUtils = new ExcelUtils();
//        excelUtils.exportExcel(response,columnList,"检测导入模板");
    }



    /**
     * 删除车辆信息
     */
    @PostMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPermi('check:detection:remove')")
    public AjaxResult delDetection(@PathVariable Long[] ids){
        return toAjax(detectionService.delDetection(ids));
    }


    /**
     * 获取检测详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('check:detection:query')")
    public AjaxResult getDetection(@PathVariable Long id){
        return success(detectionService.selectDetectionById(id));
    }


    @Log(title = "检测管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('check:detection:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        String uploadId = IdUtils.simpleUUID();
        ExcelUtil<SysCheckDetection> util = new ExcelUtil<SysCheckDetection>(SysCheckDetection.class);
        List<SysCheckDetection> detectionList = util.importExcel(file.getInputStream(),1);
        return null;
    }


}
